package fair.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import fair.constant.MessageConstant;
import fair.dao.MemberDao;
import fair.dao.OrderDao;
import fair.dao.OrderSettingsDao;
import fair.entity.Result;
import fair.pojo.Member;
import fair.pojo.Order;
import fair.pojo.OrderSetting;
import fair.service.OrderService;
import fair.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author fair_wu
 * @version v1.0
 * @package fair.service.impl
 * @date 2021-01-28 下午 21:33
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingsDao orderSettingsDao;

    @Autowired
    private MemberDao memberDao;


    @Override
    public Result order(Map map) {
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        try {
            Date date = DateUtils.parseString2Date(orderDate);
            OrderSetting orderSetting = orderSettingsDao.findByOrderDate(date);
            if (orderSetting == null){
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }//2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            if (number <= reservations){
                return new Result(false, MessageConstant.ORDER_FULL);
            }            //3、检查当前用户是否为会员，并进行预约,
            String telephone = (String) map.get("telephone");
            Member member = memberDao.findByTelephone(telephone);
            System.out.println(member.getId());
            //如果不是会员则自动完成注册
            if (member == null){
                member = new Member();
                member.setName((String)map.get("name"));
                member.setSex((String)map.get("sex"));
                member.setIdCard((String)map.get("idCard"));
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());
                memberDao.add(member);
            }else {
                //如果是会员则检查是否重复预约（同一个用户在同一天预约了同一个套餐）
                Integer memberId = member.getId();
                Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
                Order order = new Order(memberId, date, null, null, setmealId);
                List<Order> list = orderDao.findByCondition(order);
                System.out.println(list);
                if (list != null && list.size() > 0){
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
            }
            //更新当前已经预约人数
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingsDao.editReservationsByOrderDate(orderSetting);
            //添加预约信息
            Order order = new Order(member.getId(), date, (String) map.get("orderType"), Order.ORDERSTATUS_NO, Integer.parseInt((String)map.get("setmealId")));
            orderDao.add(order);
            return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }

    @Override
    public Map<String, Object> findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        if (map != null){
            Date orderDate = (Date) map.get("orderDate");
            try {
                map.put("orderDate", DateUtils.parseDate2String(orderDate));
                return map;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
